#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
APPDIR_ESC=$(echo $APPDIR | sed 's/ /\\ /g') #escaping spaces for gtkdialog
cd "${CURDIR}"

# setup working space
[ ! -d $HOME/.config/wallpaper ] && mkdir $HOME/.config/wallpaper
PREFS="$HOME/.config/wallpaper/preferences"
[ ! -f $PREFS ] && touch $PREFS
. $PREFS
[ "$INT" = "" ] && echo 'INT="15"' >> $PREFS
[ "$SLIDEDIR" = "" ] && echo 'SLIDEDIR="/usr/share/backgrounds"' >> $PREFS
#[ "$FILER" = "" ] && echo 'FILER="defaultfilemanager"' >> $PREFS
[ "$FILER" = "" ] && echo 'FILER="rox"' >> $PREFS
[ "$VIEWER" = "" ] && echo 'VIEWER="defaultimageviewer"' >> $PREFS
[ "$IMGEDITOR" = "" ] && echo 'IMGEDITOR="defaultimageeditor"' >> $PREFS
. $PREFS

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	da)	export LANGUAGE="danish" ;;
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fi)	export LANGUAGE="finnish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	ja)	export LANGUAGE="japanese" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	se)	export LANGUAGE="swedish" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. "$APPDIR/locale/english.po" #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . "$APPDIR/locale/${LANGUAGE}.po" 2> /dev/null

# what is the old background?
#grafpup uses pinboard #$HOME/.config/rox.sourceforge.net/ROX-Filer/pb_default
#puppy 4.1.2 uses $HOME/Choices/ROX-Filer/PuppyPin
OLDIMAGE="`cat $HOME/Choices/ROX-Filer/PuppyPin | grep '<backdrop ' | cut -f 2 -d '>' | cut -f 1 -d '<'`"
OLDMODE="`cat $HOME/Choices/ROX-Filer/PuppyPin | grep '<backdrop ' | cut -f 2 -d '"'`" #'
[ "$OLDMODE" = "" ] && OLDMODE="Stretch"
[ "$OLDMODE" = "Stretched" ] && OLDMODE="Stretch"
[ "$OLDMODE" = "Scaled" ] && OLDMODE="Scale"
[ "$OLDMODE" = "Tiled" ] && OLDMODE="Tile"
[ "$OLDMODE" = "Centred" ] && OLDMODE="Centre"
echo $OLDMODE > $HOME/.config/wallpaper/backgroundmode

# if run with an image file as an argument
if [ "$#" -eq 1 ];then
 case $1 in
  -play)
   [ -f $HOME/.config/sessionrc ] && . $HOME/.config/sessionrc
   [ -f $HOME/.config/wallpaper/preferences ] && . $HOME/.config/wallpaper/preferences
   [ "$SLIDEDIR" = "" ] && SLIDEDIR="/usr/share/backgrounds"
   exec "$APPDIR/slideshow" -start "$SLIDEDIR/file"
  ;;
  -about)
   exec "$APPDIR/functions" -about
  ;;
  *)
   #killall wallslide
   if [ "$(which fixwidgets)" ]; then
		echo "$1" > $HOME/.config/wallpaper/bg_img
		fixwidgets -wallpaper_setter
	else
		exec "$APPDIR/set_bg" "$1" #revert 120808
		#exec set_bg "$1" #110408
	fi
	exit
  ;;
 esac
elif [ "$#" -gt 1 ];then ##multiple arguments = several images for a slideshow
 [ "$1" = '-nextslide' ] && exec "$APPDIR/set_bg" "$2"
 killall wallslide
 [ -f $HOME/.config/sessionrc ] && . $HOME/.config/sessionrc
 [ -f $HOME/.config/wallpaper/preferences ] && . $HOME/.config/wallpaper/preferences
 SLIDEDIR="$HOME/.config/wallpaper/slideshow-dir"
 SLIDEDIR_ESC=$(echo $SLIDEDIR | sed 's/\//\\\//g')
 sed -i 's/SLIDEDIR=.*/SLIDEDIR=\"'"$SLIDEDIR_ESC"'\"/' $HOME/.config/wallpaper/preferences
 rm $SLIDEDIR_ESC/*
 mkdir -p "$SLIDEDIR"
 oldIFS=$IFS
 IFS='
'
 for FILE in $@; do
 ln -s "$FILE" "$SLIDEDIR"
 done
 IFS=$oldIFS
 exec "$APPDIR/slideshow" -start "$SLIDEDIR/file"
fi


# Radio buttons
CENTREBUTTON="<radiobutton>
<label>$LOC_CENTRE</label>
<action>echo Centre > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR_ESC/slideshow -stop</action>
<action>$APPDIR_ESC/set_bg \"\$IMAGE\"</action>
<action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
TILEBUTTON="<radiobutton>
<label>$LOC_TILE</label>
<action>echo Tile > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR_ESC/slideshow -stop</action>
<action>$APPDIR_ESC/set_bg \"\$IMAGE\"</action>
<action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
SCALEBUTTON="<radiobutton>
<label>$LOC_SCALE</label>
<action>echo Scale > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR_ESC/slideshow -stop</action>
<action>$APPDIR_ESC/set_bg \"\$IMAGE\"</action>
<action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
STRETCHBUTTON="<radiobutton>
<label>$LOC_STRETCH</label>
<action>echo Stretch > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR_ESC/slideshow -stop</action>
<action>$APPDIR_ESC/set_bg \"\$IMAGE\"</action>
<action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"

case $OLDMODE in
 Centre)
 RADIOBUTTONS="$CENTREBUTTON
 $TILEBUTTON
 $SCALEBUTTON
 $STRETCHBUTTON"
 ;;
 Tile)
 RADIOBUTTONS="$TILEBUTTON
 $CENTREBUTTON
 $SCALEBUTTON
 $STRETCHBUTTON"
 ;;
 Scale)
 RADIOBUTTONS="$SCALEBUTTON
 $CENTREBUTTON
 $TILEBUTTON
 $STRETCHBUTTON"
 ;;
 Stretch)
 RADIOBUTTONS="$STRETCHBUTTON
 $CENTREBUTTON
 $TILEBUTTON
 $SCALEBUTTON"
 ;;
esac
if [ "$OLDIMAGE" = "" ]; then
	OLDIMAGE="[none]"
	DIR="/usr/share/backgrounds"
else
	DIR="`dirname $OLDIMAGE`"
	[ "$DIR" = "$HOME/.pwidgets" ] && DIR=/usr/share/backgrounds
fi
echo "$OLDIMAGE" > $HOME/.config/wallpaper/bg_img
PREVDIR="`pwd`"

# Reset mode
echo $OLDMODE > $HOME/.config/wallpaper/backgroundmode

#add button for sidebar if installed
if [ -f $HOME/.pwidgets/pwidgetsrc ];then #icon fix 20110624, also in the main xml code for "edit" and "view"
 SIDEBAR='<button><label>Sidebar</label><input file stock="gtk-media-pause"></input><action>/usr/local/pwidgets/widgets/plugins/sidebar/sidebar &</action></button>'
fi

#show image #20110624 01micko, requires new gtkdialog
CURRENTIMG="`cat $HOME/.config/wallpaper/bg_img`"
ln -sf "$CURRENTIMG" /tmp/current_wallpaper_selection.jpg

swapimagefunc()
{
	rm -f /tmp/current_wallpaper_selection.jpg
	ISIMAGE="`echo $IMAGE|grep -iE 'jpg$|png$|jpeg$|gif|JPEG$|JPG$|tiff$|background$|svg$|SVG$'`"
	if [ "$ISIMAGE" = "" ];then IMAGE="/usr/share/doc/puppylogo96.png" #if file is not an image
	fi
	ln -sf "$IMAGE" /tmp/current_wallpaper_selection.jpg
}
export -f swapimagefunc
DIR="`echo $DIR | sed  -e 's%\n.*%%' -e 's% .*%%'`"
cd "$DIR"
export Wallpaper='
<window title="'$LOC_MAIN'" icon_name="gtk-execute" default_height="460" default_width="640">
<vbox space-expand="true" space-fill="true">
  <menubar space-expand="false" space-fill="false">
    <menu>
      <menuitem stock="gtk-media-play" label="'$LOC_SLIDESHOW'">
        <action>'$APPDIR_ESC'/slideshow -start "$IMAGE" &</action>
      </menuitem>
      <menuitem stock="gtk-preferences" label="'$LOC_OPTIONS'">
        <action>'$APPDIR_ESC'/functions -preferences &</action>
      </menuitem>
      <separator></separator>
      <menuitem stock="gtk-quit" label="'$LOC_QUIT'">
        <action>EXIT:abort</action>
      </menuitem>
      <label>'$LOC_FILE'</label>
    </menu>
    <menu>
      <menuitem stock="gtk-about">
        <action>'$APPDIR_ESC'/functions -about</action>
      </menuitem>
      <label>'$LOC_HELP'</label>
    </menu>
  </menubar>
  <hbox space-expand="true" space-fill="true">
    <vbox space-expand="true" space-fill="true">
      <frame '$LOC_FILE_FRAME'>
        <chooser space-expand="true" space-fill="true">
          <default>'$DIR'</default>
          <variable>IMAGE</variable>
          <action>'$APPDIR_ESC'/slideshow -stop</action>
          <action>'$APPDIR_ESC'/set_bg "$IMAGE"</action>
          <action>echo "$IMAGE" > '$HOME'/.config/wallpaper/bg_img</action>
          <action when="update-preview">swapimagefunc</action>
          <action when="update-preview">refresh:PIXMAP</action> 
       </chooser>
     </frame>
    </vbox>
    <hbox space-expand="false" space-fill="false">
      <frame>
        <vbox space-expand="true" space-fill="true">
          <pixmap space-expand="false" space-fill="false">
            <width>160</width>
            <variable>PIXMAP</variable>
            <input file>/tmp/current_wallpaper_selection.jpg</input>
          </pixmap>
          <text space-expand="true" space-fill="true"><label>""</label></text>
          <vbox spacing="2" space-expand="false" space-fill="false">
            <text><label>'$LOC_MODE'</label></text>
            <hseparator></hseparator>
            '$RADIOBUTTONS'
            <text><label>""</label></text>
          </vbox>
          <button height-request="70" image-position="2">
            <label>'$LOC_APPLY'</label>
            <input file stock="gtk-apply"></input>
            <action>'$APPDIR_ESC'/slideshow -stop</action>
            <action>'$APPDIR_ESC'/set_bg "$IMAGE"</action>
            <action>echo "$IMAGE" > '$HOME'/.config/wallpaper/bg_img</action>
            <action>which fixwidgets 2>&1 > /dev/null && fixwidgets -wallpaper_setter &</action>
            <action type="refresh">TEXT</action>
          </button>
        </vbox>
      </frame>
    </hbox>
  </hbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <input file stock="gtk-quit"></input>
      <label>'$LOC_CLOSE'</label>
    </button>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button>
      <label>'$LOC_CLEAR'</label>
      <input file stock="gtk-clear"></input>
      <action>'$APPDIR_ESC'/slideshow -stop</action>
      <action>'$APPDIR_ESC'/set_bg -clear</action>
      <action type="refresh">TEXT</action>
    </button>
    '$SIDEBAR'
    <button>
      <label>'$LOC_EDIT'</label>
      <input file stock="gtk-edit"></input>
      <action>'$IMGEDITOR' "$IMAGE" &</action>
    </button>
    <button height-request="30">
      <label>'$LOC_VIEW'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon image_viewer.svg`"'
      <action>'$VIEWER' "$IMAGE" &</action>
    </button>
    <button>
      <label>'$LOC_FILER'</label>
      <input file stock="gtk-directory"></input>
      <action>dirname "$IMAGE" > '$HOME'/.config/wallpaper/bgdir</action>
      <action>'$APPDIR'/functions -filer</action>
    </button>
  </hbox>
</vbox>
</window>
'

gtkdialog -p Wallpaper

cd "$PREVDIR"
